//
//  DFSearchBar.swift
//  新浪微博
//
//  Created by dafei on 16/5/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

  //@IBInspectable: 实时显示属性的设置
  //必须是自定义视图


import UIKit

class DFSearchBar: UIView {

//    @IBInspectable var cornerRadius: CGFloat {
    
//        get {
//        
//            return layer.cornerRadius
//        
//        }
//        set {
//            
//            layer.cornerRadius = newValueself.rightcCon.constant  = 0
//        }
//        
//  }
    
    
    @IBOutlet weak var seachTF: UITextField!
    
    
    @IBOutlet weak var cancleBtn: UIButton!

 
    @IBOutlet weak var rightcCon: NSLayoutConstraint!

    
    //提供加载nib文件的 类方法
    //在func 前加class 就是类方法
    class func loadSearchBar() -> DFSearchBar{
    
        let searchView = NSBundle.mainBundle().loadNibNamed("DFSearchBar", owner: nil, options: nil).last as! DFSearchBar
        
        return searchView
    
    }
    
    // 在awakefromnib 做初始设置
    override func awakeFromNib() {
        
        //设置宽度
        //要修改大小通过size进行修改
        self.bounds.size.width = ScreenWidth
        //文本框的leftview
        seachTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //显示样式
        seachTF.leftViewMode = .Always
        //设置的frame
        seachTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        
        //设置视图显示模式
        seachTF.leftView?.contentMode = .Center
        
//        self.layer.cornerRadius = 20
//        
//        self.layer.masksToBounds = true
//        
//        //设置边线颜色
//        self.layer.borderColor = UIColor.orangeColor().CGColor
//        self.layer.borderWidth = 2
        
        
        
    }
    
    
    @IBAction func textFiledBeginEditin(sender: AnyObject) {
        
        //修改约束
        //0.25系统默认动画时间
        
        UIView.animateWithDuration(0.25) { 
            self.rightcCon.constant = self.cancleBtn.bounds.width
            //提前强制刷新视图
            self.layoutIfNeeded()
        }
        
    }

    
    
    @IBAction func cancleBtnClick(sender: AnyObject) {
        
        //结束编辑状态  (可以循环点击)
        self.endEditing(true)
        self.rightcCon.constant  = 0
        UIView.animateWithDuration(0.25) {
            
            self.layoutIfNeeded()
        }
        //self.endEditing(true)
        
//        seachTF.resignFirstResponder()
       
    }
  
    
    
    
    
}
